package com.taplinker.client.widget.image;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.taplinker.client.R;
import com.taplinker.client.core.image.ImageManager;
import com.taplinker.core.VersionSelecter;

public class TaplinkerImageView extends ImageView implements IImageLoader {
	private ImageOptions options = new ImageOptions();
	private static final int PROGRESS_MAX = 100;
	private Paint paint;
	private String url;
	
	private boolean showMaxImage;
	private boolean showProgress;
	private boolean shouldFixImage;
	// 圆角半径
	private int roundX;
	private int roundY;
	// 边框
	int roundStrokeWidth;
	int roundStokeColor;
	// 进度
	private int progressColor;
	private int progress;
	private int progressTextColor;
	private int progressTextSize;
	// 进度圆环
	private int ringWidth;
	private int ringColor;
	
	public TaplinkerImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.paint = new Paint();
		this.paint.setAntiAlias(true);
		
		initLayerType();
		initStyledAttributes(context, attrs);
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void initLayerType() {
		if (VersionSelecter.hasAPI11()) {
			setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
	}
	
	private void initStyledAttributes(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TaplinkerImageView);
		
		showMaxImage = ta.getBoolean(R.styleable.TaplinkerImageView_showMaxImage, true);
		showProgress = ta.getBoolean(R.styleable.TaplinkerImageView_showProgress, false);
		shouldFixImage = ta.getBoolean(R.styleable.TaplinkerImageView_shouldFixImage, true);
		
		roundX = ta.getInt(R.styleable.TaplinkerImageView_roundX, 0);
		roundY = ta.getInt(R.styleable.TaplinkerImageView_roundY, 0);
		
		roundStrokeWidth = ta.getInt(R.styleable.TaplinkerImageView_roundStokeWidth, 0);
		roundStokeColor = ta.getColor(R.styleable.TaplinkerImageView_roundStokeColor, 0);
		
		progressColor = ta.getInt(R.styleable.TaplinkerImageView_progressColor, Color.WHITE);
		progress = ta.getInt(R.styleable.TaplinkerImageView_progress, 0);
		progressTextColor = ta.getInt(R.styleable.TaplinkerImageView_progressTextColor, Color.WHITE);
		progressTextSize = ta.getInt(R.styleable.TaplinkerImageView_progressTextSize, 13);
		
		ringWidth = ta.getInt(R.styleable.TaplinkerImageView_ringWidth, 2);
		ringColor = ta.getInt(R.styleable.TaplinkerImageView_ringColor, Color.WHITE);
		
		ta.recycle();
	}
	
	public TaplinkerImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public TaplinkerImageView(Context context) {
		this(context, null);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		drawProgress(canvas);
		Path clipPath = new Path();
		int w = getWidth();
		int h = getHeight();
		if(roundX != 0 || roundY != 0) {
			// 绘制圆角
			paint.setColor(roundStokeColor);
			if(roundStrokeWidth != 0) {
				paint.setStrokeWidth(roundStrokeWidth);
			}
			paint.setAntiAlias(true);
			paint.setStyle(Paint.Style.STROKE);
			clipPath.addRoundRect(new RectF(0, 0, w, h), roundX, roundY, Path.Direction.CW);
			canvas.clipPath(clipPath);
		}
		
		super.onDraw(canvas);
		if(roundX != 0 || roundY != 0) {
			clipPath = new Path();
			clipPath.addRoundRect(new RectF(0, 0, w, h), roundX, roundY, Path.Direction.CW);
			canvas.drawPath(clipPath, paint);
		}
	}
	
	private void drawProgress(Canvas canvas){
		if(showProgress) {
			int centerX = getWidth() / 2;
			int centerY = getHeight() / 2;
			int radios = 18;
			// 绘制进度圆环
			this.paint.setStyle(Paint.Style.STROKE);
			this.paint.setColor(ringColor);
			this.paint.setStrokeWidth(ringWidth);
			canvas.drawCircle(centerX, centerY, radios, this.paint);
			RectF oval = new RectF(centerX - radios, centerX - radios, centerX	+ radios, centerX + radios);
			this.paint.setColor(progressColor);
			canvas.drawArc(oval, 90, 360 * progress / PROGRESS_MAX, false, paint);
			this.paint.setStyle(Paint.Style.FILL);
			this.paint.setColor(progressTextColor);
			this.paint.setStrokeWidth(0);
			this.paint.setTextSize(progressTextSize);
			this.paint.setTypeface(Typeface.DEFAULT);
			// 绘制进度文本
			String textProgress = (int) (1000 * (progress / (10.0 * PROGRESS_MAX))) + "%";
			float textWidth = paint.measureText(textProgress);
			canvas.drawText(textProgress, centerX - textWidth / 2, centerY + progressTextSize / 2, paint);
		}
		if(showProgress && progress == PROGRESS_MAX) {
			hideProgress(1);
		}
		if(showProgress && progress == 0) {
			hideProgress(1);
		}
	}
	
	private void hideProgress(int when) {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				showProgress = false;
				invalidate();
			}
		}, 1000 * when);
	}

	@Override
	public int getImageWidth() {
		return getWidth();
	}
	
	@Override
	public int getImageHeight() {
		return getHeight();
	}
	
	@Override
	public void loadStart(int resId) {
		if(resId == 0) {
			resId = R.drawable.img_loading;
		}
		setImageResource(resId);
	}
	
	@Override
	public void setUrl(String url) {
		this.url = url;
		ImageManager.getInstance().loadImg(url, this, null);
	}
	
	@Override
	public String getUrl() {
		return url;
	}

	@Override
	public void loadError(int resId) {
		if(resId == 0) {
			resId = R.drawable.img_loading_error;
		}
		setImageResource(resId);
	}

	@Override
	public void loadComplete(Bitmap bitmap) {
		if(bitmap != null) {
			setImageBitmap(bitmap);
		}
	}
	
	@Override
	public void updateProgress(long plan, long cur) {
		progress = (int) (plan / cur);
		invalidate();
	}

	@Override
	public boolean isShowMaxImage() {
		return showMaxImage;
	}

	@Override
	public boolean isShowProgress() {
		return showProgress;
	}

	@Override
	public boolean fixImage() {
		return shouldFixImage;
	}

	@Override
	public ImageOptions getImageOptions() {
		return options;
	}

	@Override
	public void setImageOptions(ImageOptions options) {
		this.options = options;
	}
	
	@Override
	public void setClickListener(OnClickListener onClickListener) {
		if(onClickListener != null) {
			this.setOnClickListener(onClickListener);
		}
	}
	
}
